package com.pilipili.zeus.educenter.controller;


import com.pilipili.commonutils.JwtUtils;
import com.pilipili.commonutils.Result;
import com.pilipili.zeus.educenter.entity.UcenterMember;
import com.pilipili.zeus.educenter.entity.vo.Register;
import com.pilipili.commonutils.vo.User;
import com.pilipili.zeus.educenter.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 会员表 前端控制器
 *
 * @author Unlimited_Blade_Works
 * @since 2021-05-31
 */
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterService;
    @ApiOperation(value = "用户登录")
    @PostMapping("login")
    public Result userLogin(@RequestBody UcenterMember user){
        String token=ucenterService.login(user);
        return Result.ok().data("token",token);
    }
    @ApiOperation(value = "用户注册")
    @PostMapping("register")
    public Result userRegister(@RequestBody Register user){
        ucenterService.register(user);
        return Result.ok();
    }
    @ApiOperation(value = "读取用户登录状态")
    @GetMapping("token")
    public Result getUserInfo(HttpServletRequest request){
        String id = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember user = ucenterService.getById(id);
        return Result.ok().data("userInfo",user);
    }
    //根据token字符串获取用户信息
    @ApiOperation(value = "读取用户信息")
    @PostMapping("getUserInfo/{id}")
    public User getUserInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = ucenterService.getById(id);
        User memeber = new User();
        BeanUtils.copyProperties(ucenterMember,memeber);
        return memeber;
    }
    @ApiOperation(value = "读取用户注册人数")
    @GetMapping("countRegisterNum/{date}")
    public Result countRegisterNum(@PathVariable String date){
        Integer num=ucenterService.countRegisterNum(date);
        return Result.ok().data("num",num);
    }
}

